package com.hkts.naturalgasauthentication.utils.sms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameSendSms
 * @Description
 * @Author yck
 * @Date2021-08-03 16:38
 * @Version V1.0
 **/
@Component
public class SendSms {
    @Autowired
  private   SmsConfig smsConfig;
    private  final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {


        @Override
        public boolean verify(String hostname, SSLSession session) {


            return true;


        }


    };

    public  Map<String, String> sendMessage(String msisdn,String code) throws Exception {
        // ip：port根据实际情况填写
        String url = smsConfig.getUrl();
        String smsTemplateId = smsConfig.getSmsTemplateId();
        String accout = smsConfig.getAccount(); //实际账号
        String passward = smsConfig.getPassword(); //实际密码
        //设置验证码
        Map<String, String> templateParas = new HashMap<String, String>();
        Map<String, String> resultMap = new HashMap<String, String>();
        templateParas.put("code", code);

        // If the request body does not contain the signature name, set signature to null.
        Map<String, Object> body = buildRequestBody(msisdn, smsTemplateId, templateParas, accout, passward);
        HttpsURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        NamedPatameter.trustAllHttpsCertificates();
        try {
            URL realUrl = new URL(url);
            connection = (HttpsURLConnection) realUrl.openConnection();
            connection.setHostnameVerifier(DO_NOT_VERIFY);
            connection.setDoInput(true); // 设置可输入
            connection.setDoOutput(true); // 设置该连接是可以输出的
            connection.setRequestMethod("POST"); // 设置请求方式
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            // connection.connect();
            ObjectMapper objectMapper = new ObjectMapper();
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
            pw.write(objectMapper.writeValueAsString(body));
            pw.flush();
            pw.close();


            br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            int status = connection.getResponseCode();
            if (200 == status) { // 200
                is = connection.getInputStream();
            } else { // 400/401
                is = connection.getErrorStream();
            }
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;
            StringBuilder result = new StringBuilder();
            while ((line = br.readLine()) != null) { // 读取数据
                result.append(line + "\n");
            }
            connection.disconnect();
            Gson gson=new Gson();
            System.out.println(result.toString());
            resultMap=gson.fromJson(result.toString(),resultMap.getClass());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
                if (null != br) {
                    br.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultMap;
    }

    // msisdn, smsTemplateId, paramValues, countryID
    public  Map<String, Object> buildRequestBody(String msisdn, String smsTemplateId,
                                                       Map<String, String> paramValues, String accout, String passward) {
        if (null == msisdn || null == smsTemplateId || null == accout || null == passward) {
            System.out.println(
                    "buildRequestBody(): mobiles, templateId or templateParas or account or password is null.");
            return null;
        }


        Map<String, Object> map = new HashMap<String, Object>();
        List<MtSmsMessage> requestLists = new ArrayList<MtSmsMessage>();
        MtSmsMessage mtSmsMessage = new MtSmsMessage();
        List<String> mobiles = new ArrayList<String>();
        mobiles.add(msisdn);
        mtSmsMessage.setMobiles(mobiles);
        mtSmsMessage.setTemplateId(smsTemplateId);
        mtSmsMessage.setTemplateParas(paramValues);
        mtSmsMessage.setSignature("【顺满满】");
        requestLists.add(mtSmsMessage);
        map.put("account", accout);
        map.put("password", passward);
        map.put("requestLists", requestLists);
        return map;
    }

    public static class MtSmsMessage {
        List<String> mobiles;


        String templateId;


        Map<String, String> templateParas;


        String signature;


        String messageId;


        String extCode;


        List<NamedPatameter> extendInfos;


        /**
         * 返回 mobiles
         *
         * @return mobiles值
         */
        public List<String> getMobiles() {
            return mobiles;
        }


        /**
         * 对mobiles进行赋值
         *
         * @param mobiles mobiles值
         */
        public void setMobiles(List<String> mobiles) {
            this.mobiles = mobiles;
        }


        /**
         * 返回 templateId
         *
         * @return templateId值
         */
        public String getTemplateId() {
            return templateId;
        }


        /**
         * 对templateId进行赋值
         *
         * @param templateId templateId值
         */
        public void setTemplateId(String templateId) {
            this.templateId = templateId;
        }


        /**
         * 返回 templateParas
         *
         * @return templateParas值
         */
        public Map<String, String> getTemplateParas() {
            return templateParas;
        }


        /**
         * 对templateParas进行赋值
         *
         * @param templateParas templateParas值
         */
        public void setTemplateParas(Map<String, String> templateParas) {
            this.templateParas = templateParas;
        }


        /**
         * 返回 signature
         *
         * @return signature值
         */
        public String getSignature() {
            return signature;
        }


        /**
         * 对signature进行赋值
         *
         * @param signature signature值
         */
        public void setSignature(String signature) {
            this.signature = signature;
        }


        /**
         * 返回 messageId
         *
         * @return messageId值
         */
        public String getMessageId() {
            return messageId;
        }


        /**
         * 对messageId进行赋值
         *
         * @param messageId messageId值
         */
        public void setMessageId(String messageId) {
            this.messageId = messageId;
        }


        /**
         * 返回 extCode
         *
         * @return extCode值
         */
        public String getExtCode() {
            return extCode;
        }


        /**
         * 对extCode进行赋值
         *
         * @param extCode extCode值
         */
        public void setExtCode(String extCode) {
            this.extCode = extCode;
        }


        /**
         * 返回 extendInfos
         *
         * @return extendInfos值
         */
        public List<NamedPatameter> getExtendInfos() {
            return extendInfos;
        }


        /**
         * 对extendInfos进行赋值
         *
         * @param extendInfos extendInfos值
         */
        public void setExtendInfos(List<NamedPatameter> extendInfos) {
            this.extendInfos = extendInfos;
        }
    }
}
